import { SAVA_USER_INFO, DELETE_USER_INFO } from '../action_type'

//尝试从localStorage 中读取之前保存的信息 
let user = JSON.parse(localStorage.getItem('user'))
let token = localStorage.getItem('token')

//初始化 userInfo数据 

let initState = {
    //若有值 取出使用, 没有值 则为空 
    user: user || '',
    token: token || '',
    isLogin: user && token ? true : false
}




export default function test(preState = initState, action) {
    const { type, data } = action
    let newState
    switch (type) {
        case SAVA_USER_INFO:  //保存user 和token    
            const { user, token } = data
            newState = { user, token, isLogin: true }
            return newState

        case DELETE_USER_INFO:

            newState = { user: "", token: '', isLogin: false }

            return newState

        default:
            return preState
    }
}




